package com.zh.yqbj.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.zh.yqbj.dto.*;
import com.zh.yqbj.exception.accountExcption.*;
import com.zh.yqbj.VO.LoginVO;
import com.zh.yqbj.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;


/**
 * 账号相关控制器
 *
 * @author zh
 * @date 2022/3/20 15:23
 */
@Slf4j
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 获取验证码，根据类型
     *
     * @param sendCodeDTO 参数为空则随机生成，手机号则发送短信，邮箱则发送邮件
     * @return 验证码的key
     * @date 2023/03/23 14:00
     */
    @PostMapping("/sendCode")
    public SaResult sendCode(@RequestBody SendCodeDTO sendCodeDTO) throws MessagingException,
            UnsupportedEncodingException, NoMoneyException {
        String imageBase64 = accountService.sendCode(sendCodeDTO);
//        返回的key在用户点击登录注册时，加入UserDTO携带回后端
        return SaResult.ok("验证码发送成功").setData(imageBase64);
    }

    /**
     * 用户登录接口
     *
     * @param userLoginDTO userLoginDTO
     * @return userVO
     * @date 2023/03/23 14:00
     */
    @PostMapping("/login/user")
    public SaResult login(@RequestBody UserLoginDTO userLoginDTO) throws UnknownAccountException,
            ErrorPasswordException, ErrorCodeException, ErrorPhonePatternException, ErrorEmailPatternException,
            RregisterException {
        LoginVO loginVO = accountService.userLogin(userLoginDTO);
        SaResult saResult = SaResult.ok("登录成功");
        saResult.setData(loginVO);
        return saResult;
    }

    /**
     * 用户登出接口
     *
     * @return void
     * @date 2023/03/23 14:00
     */
    @SaCheckLogin
    @GetMapping("/logout")
    public SaResult logout() {
        accountService.userLogout();
        return SaResult.ok("已退出登录");
    }

    /**
     * 用户注册接口
     *
     * @param userRegisterDTO 用户名 密码 再密码 邮箱 手机号 验证码key 验证码
     * @return 注册成功
     * @throws ErrorUsernamePatternException 账号格式错误
     * @throws ErrorRepasswordException      密码格式错误
     * @throws ErrorCodeException            验证码错误
     * @throws ErrorPhonePatternException    手机号格式错误
     * @throws ErrorEmailPatternException    邮箱格式错误
     * @throws RregisterException            注册出错
     * @throws DuplicateAccountException     账号已存在
     * @date 2023/03/23 14:00
     */
    @PostMapping("/register")
    public SaResult register(@RequestBody UserRegisterDTO userRegisterDTO) throws ErrorUsernamePatternException,
            ErrorRepasswordException, ErrorCodeException, ErrorPhonePatternException,
            ErrorEmailPatternException, RregisterException, DuplicateAccountException {
        accountService.userRegister(userRegisterDTO);
        return SaResult.ok("注册成功");
    }

    @PostMapping("/binding/email")
    @SaCheckLogin
    public SaResult bindingEmail(@RequestBody BindEmailDTO bindEmailDTO) throws DuplicateEmailException,
            ErrorCodeException, ErrorEmailPatternException {
        accountService.bindingEmail(bindEmailDTO);
        return SaResult.ok("绑定成功");
    }

    @PostMapping("/binding/phone")
    @SaCheckLogin
    public SaResult bindingPhone(@RequestBody BindPhoneDTO bindPhoneDTO) throws DuplicatePhoneException,
            ErrorCodeException, ErrorPhonePatternException, DuplicateEmailException, ErrorEmailPatternException {
        String msg = accountService.bindingPhone(bindPhoneDTO);
        return SaResult.ok(msg);
    }

    /**
     * 修改个人信息，不包括修改密码
     *
     * @param userUpdateDTO userUpdateDTO
     */
    @PutMapping("/update")
    public SaResult updateUser(@RequestBody UserUpdateDTO userUpdateDTO) throws ErrorPhonePatternException,
            ErrorCodeException, ErrorEmailPatternException, ErrorUsernamePatternException, ErrorRepasswordException,
            DuplicateAccountException, DuplicatePhoneException, DuplicateEmailException {
        log.info("userUpdateDTO:{}", userUpdateDTO);
        String msg = accountService.updateUser(userUpdateDTO);
        return SaResult.ok(msg);
    }

    /**
     * 区分与修改个人信息
     *
     * @param updatePwdDTO
     * @return
     * @throws UnknownAccountException
     * @throws ErrorPasswordException
     * @throws ErrorRepasswordException
     * @throws ErrorCodeException
     */
    @PutMapping("/updatePwd")
    @SaCheckLogin
    public SaResult updatePwd(@RequestBody UpdatePwdDTO updatePwdDTO) throws UnknownAccountException,
            ErrorPasswordException, ErrorRepasswordException, ErrorCodeException {
        accountService.updatePwd(updatePwdDTO);
        return SaResult.ok("密码修改成功");
    }

    @PostMapping("/refund")
    public SaResult refundPwd(@RequestBody RefundPwdDTO refundPwdDTO) throws ErrorPhonePatternException,
            ErrorCodeException, UnBindingPhoneException, UnknownAccountException, UnBindingEamilException,
            ErrorEmailPatternException {
        String msg = accountService.refundPwd(refundPwdDTO);
        return SaResult.ok(msg);
    }

    /**
     * 测试接口
     *
     * @param request 请求
     * @return 测试结果
     */
    @GetMapping("/about")
    public SaResult about(HttpServletRequest request) {
        UserAgent parse = UserAgentUtil.parse(request.getHeader("User-Agent"));
        System.out.println(parse.getOs());
        String clientIP = ServletUtil.getClientIP(request);
        System.out.println(clientIP);
        //  获取用户设备信息 完成了
        return SaResult.data("请求成功");
    }
}
